package com.ziven.learn.main

import com.ziven.easygo.ui.AbstractBaseActivity
import com.ziven.easygo.util.LogHelper
import com.ziven.learn.R
import com.ziven.learn.view.SortDrawView

class SortLearnActivity : AbstractBaseActivity() {
    private val data: IntArray =
        IntArray(10) {
            when (it) {
                0 -> 5
                1 -> 7
                2 -> 4
                3 -> 9
                else -> it
            }
        }

    override fun obtainLayout(): Any = R.layout.activity_main_sort

    override fun initLayout() {
        val sortView: SortDrawView = getView(R.id.sort_draw_view)
        getViewHelper(R.id.button1).setOnClickListener { sort(it.id, data, sortView) }
        getViewHelper(R.id.button2).setOnClickListener { sort(it.id, data, sortView) }
        getViewHelper(R.id.button3).setOnClickListener { sort(it.id, data, sortView) }
        getViewHelper(R.id.button4).setOnClickListener { sort(it.id, data, sortView) }
        getViewHelper(R.id.button5).setOnClickListener { sort(it.id, data, sortView) }
        getViewHelper(R.id.button6).setOnClickListener { sort(it.id, data, sortView) }
    }

    private fun sort(
        id: Int,
        sort: IntArray,
        view: SortDrawView,
    ) {
        if (view.isDoing()) {
            LogHelper.log("SortLearnActivityTag", "is doing...")
            return
        }
        LogHelper.log("SortLearnActivityTag", "id=$id sort=$sort")
        view.putData(sort)
        when (id) {
            R.id.button1 -> sort1(view.getData(), view)
            R.id.button2 -> sort2(view.getData(), view)
            R.id.button3 -> sort3(view.getData(), view)
            R.id.button4 -> sort4(view.getData(), view)
            R.id.button5 -> sort5(view.getData(), view)
            R.id.button6 -> sort6(view.getData(), view)
        }
        getViewHelper(R.id.text_view).setText(view.getText())
        view.drawAnim(true)
    }

    private fun sort1(
        sort: IntArray,
        view: SortDrawView,
    ) {
        val size = sort.size
        for (i in 0 until size) {
            var minIndex = i
            for (j in (i + 1) until size) {
                if (sort[j] < sort[minIndex]) {
                    minIndex = j
                }
            }
            if (minIndex != i) view.swap(minIndex, i)
        }
    }

    private fun sort2(
        sort: IntArray,
        view: SortDrawView,
    ) {

    }

    private fun sort3(
        sort: IntArray,
        view: SortDrawView,
    ) {

    }

    private fun sort4(
        sort: IntArray,
        view: SortDrawView,
    ) {

    }

    private fun sort5(
        sort: IntArray,
        view: SortDrawView,
    ) {

    }

    private fun sort6(
        sort: IntArray,
        view: SortDrawView,
    ) {

    }

    override fun destroyLayout() = Unit
}
